import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Wclcome from '../components/Welcome.vue'
import Users from '../components/user/Users.vue'
import Roles from '../components/roles/Roles.vue'
import Cate from '../components/goods/Cate.vue'
import Params from '../components/goods/Params.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/Home',
    component: Home,
    redirect: "/welcome",
    children: [
      { path: '/welcome', component: Wclcome },
      { path: '/users', component: Users },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
    ]
  }
]


const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // 如果用户访问的登陆页面，直接放行
  if (to.path === "/login") return next();
  //   如果没有登陆，即没有token，则强制跳转到登陆页面
  if (!window.sessionStorage.getItem("token")) {
    return next("/login");
  } else {
    return next();
  }
});

export default router
